package com.shanling.shanlingcontroller.ui.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkplay.lpvr.avslib.LPAVSManager;
import com.linkplay.lpvr.avslib.LPAlexaAccount;
import com.linkplay.lpvr.lpvrbean.ResponseEntity;
import com.linkplay.lpvr.lpvrcallback.IotCloudCallback;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.view.MyDilalogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TryActivity extends BaseAppCompatActivity implements MyDilalogUtils.OnDialogClickListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_toalexa)
    TextView tvToalexa;
    @BindView(R.id.bt_logout)
    Button btLogout;
    private MyDilalogUtils dilalogUtils;
    private LPAlexaAccount lpAlexaAccount;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_try;
    }

    @Override
    protected void initViewsAndEvents() {
        SpannableStringBuilder navBuilder = new SpannableStringBuilder(getString(R.string.alexa_hint6));
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources()
                .getColor(R.color.alexa_color));
        navBuilder.setSpan(colorSpan, navBuilder.length()-16, navBuilder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvToalexa.setText(navBuilder);
        dilalogUtils = new MyDilalogUtils(this, R.style.myDialog, getString(R.string.Amazon_account), getString(R.string.logouthint));
        dilalogUtils.setOnDialogClickListener(this);


    }
    @Override
    protected void initData() {
        lpAlexaAccount=LPAVSManager.getInstance(this).getAccount();

    }
    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    @OnClick({R.id.iv_back, R.id.tv_toalexa, R.id.bt_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_toalexa:
                PackageManager packageManager = this.getApplicationContext().getPackageManager();
                Intent intent= packageManager.getLaunchIntentForPackage("com.amazon.dee.app");
                if (intent != null) {
                    this.getApplicationContext().startActivity(intent);
                }else {
                    ToastUtils.showToast(this,R.string.AmazonAlxaapp_notinstalled);
                }
                break;
            case R.id.bt_logout:
                dilalogUtils.show();
                dilalogUtils.setCancelable(false);
                break;
        }
    }

    @Override
    public void onSure() {
        if(lpAlexaAccount==null){
            return;
        }
        lpAlexaAccount.logOut();
        finish();
    }

    @Override
    public void onCancel() {

    }
}
